(function($){
	$.fn.datagrid = function(options){
		var tableObj = this;
		var defaults = {
			pagination:true,	//是否启用分页功能
			pageSize:10,	//分页每页大小
			pageCount:5,	//分页标签数量
			url:"",	//json数据地址
			data:null,	//需要post的数据(用于搜索)
			columns:[],	//表格列对应的每条json数据属性名
			gridId:"1"	//数据表ID，初始化时默认会取table的id属性，当有多个表格时，若table没有id属性，需要指定，以区分
		};
		var plugOpts = $.extend(defaults,options);
		var tableId = tableObj.attr("id");
		if(!tableId || tableId=="undefined"){
			tableId = plugOpts.gridId;
		}
		
		var queryData = function(targetPage){	//ajax post请求数据
			var postData = {};
			if(plugOpts.data!=null){
				if($.isFunction(plugOpts.data)){
					plugOpts.data(postData);
				}else{
					postData = plugOpts.data;
				}
			}
			
			if(plugOpts.pagination){
				var currentPage = targetPage?targetPage:1;
				postData["currentPage"] = currentPage;
				postData["pageSize"] = plugOpts.pageSize;
			}
			$.post(plugOpts.url,postData,function(jsonResult){
				if(jsonResult.code==1){
					clearTable();
					var pageData = jsonResult.data;
					var pageInfo = pageData.page;
					var pageList = pageData.list;
					initTableData(pageList);
					if(plugOpts.pagination){
						initPaginationBar(pageInfo);
					}
				}
			});
		};
		
		var clearTable = function(){	//清空表格数据
			tableObj.find("tbody").remove();
			$("#"+tableId+"_paginate").remove();
			$("#"+tableId+"_info").remove();
			var checkboxObjs = tableObj.find(":checkbox");
			checkboxObjs.prop("checked",false);
			$.uniform.update(checkboxObjs);
		};
		
		var initTableData = function(pageList){	//解析json数据显示在表格上
			if(plugOpts.columns.length>0){
				if(pageList!=null && pageList.length>0){
					var tbodyHtml = "<tbody>";
					for(var i=0; i<pageList.length; i++){
						tbodyHtml += "<tr class=\""+(i%2==0?"odd":"even")+"\">";
						var dataObj = pageList[i];
						for(var j=0; j<plugOpts.columns.length; j++){
							tbodyHtml += "<td>";
							var colInfo = plugOpts.columns[j];//{"name":"...","render":function(cellData){}}
							var dataNameStr = colInfo["name"];	//属性名	
							var dataNames = dataNameStr.split(".");
							var dataVal = dataObj[dataNames[0]];//属性值
							var dataVals = dataVal+'';						
							dataVal  = dataVals.replace('undefined','');		
							if(dataNames.length>1){
								for(var d=1; d<dataNames.length; d++){
									var tmpDataName = dataNames[d];
									dataVal = dataVal[tmpDataName];	

								}
							}
							var time = colInfo["time"]?function(cellData){
								return new Date(cellData).Format("yyyy-MM-dd hh:mm:ss");
							}:function(cellData){
								return cellData;
							};
							
							var render = colInfo["render"]?colInfo["render"]:function(cellData){
								return cellData;
							};
							tbodyHtml += time(render(dataVal));
							
							tbodyHtml += "</td>";
						}
						tbodyHtml += "</tr>";
					}
					tbodyHtml += "</tbody>";
					tableObj.append(tbodyHtml);
					tableObj.find(":checkbox").uniform();
					if($("#"+tableId+"_wrapper").size()==0){
						tableObj.wrap("<div class=\"dataTables_wrapper\" id=\""+tableId+"_wrapper\"></div>");
					}
				}else{
					var emptyDataTrLen = tableObj.find("tr").first().children().size();
					var tbodyHtml = "<tbody>";
					tbodyHtml += "<tr><td colspan=\""+emptyDataTrLen+"\" style=\"text-align: center;\">暂无数据</td></tr>";
					tbodyHtml += "</tbody>";
					tableObj.append(tbodyHtml);
				}
			}else{
				alert("未指定columns属性！");
			}
		};
		
		var initPaginationBar = function(pageInfo){	//初始分页条
			var currentPage = pageInfo.currentPage;			
			var pageSize = pageInfo.pageSize;			
			var recordAmount = pageInfo.recordAmount;			
			var showCount = plugOpts.pageCount;
			if(recordAmount>0){
				var barHtml = "<div class=\"dataTables_paginate\" id=\""+tableId+"_paginate\">";
				var pageAmount = Math.floor(recordAmount/pageSize);
				if(recordAmount%pageSize!=0){
					pageAmount ++;
				}
				if(pageAmount==0){
					pageAmount=1;
				}
				if(showCount>pageAmount){
					showCount = pageAmount;
				}							
				if(currentPage==1){
					barHtml += "<span class=\"first paginate_button\" id=\""+tableId+"_first\" style=\"cursor: default;\">首页</span>";
					barHtml += "<span class=\"previous paginate_button\" id=\""+tableId+"_previous\" style=\"cursor: default;\">上页</span>";
				}else{
					barHtml += "<span pagenum=\"1\" class=\"first paginate_button\" id=\""+tableId+"_first\">首页</span>";
					barHtml += "<span pagenum=\""+(currentPage-1)+"\" class=\"previous paginate_button\" id=\""+tableId+"_previous\">上页</span>";
				}
				var startPageNum = (currentPage+Math.floor(showCount/2))<showCount?1:(currentPage+Math.floor(showCount/2)+1)-showCount;
				var endPageNum = startPageNum+showCount-1;
				if(endPageNum>pageAmount){
					endPageNum = pageAmount;
					startPageNum = endPageNum-showCount+1;
				}
				if(startPageNum<1)
					startPageNum = 1;
				barHtml += "<span>";
				for(var i=startPageNum; i<=endPageNum; i++){
					if(i==currentPage){
						barHtml += "<span class=\"paginate_active\">"+i+"</span>";
					}else{
						barHtml += "<span pagenum=\""+i+"\" class=\"paginate_button\">"+i+"</span>";
					}
				}
				barHtml += "</span>";
				if(currentPage<pageAmount){
					barHtml += "<span pagenum=\""+(currentPage+1)+"\" class=\"next paginate_button\" id=\""+tableId+"_next\">下页</span>";
					barHtml += "<span pagenum=\""+pageAmount+"\" class=\"last paginate_button\" id=\""+tableId+"_last\">尾页</span>";
				}else{
					barHtml += "<span class=\"next paginate_button\" id=\""+tableId+"_next\" style=\"cursor: default;\">下页</span>";
					barHtml += "<span class=\"last paginate_button\" id=\""+tableId+"_last\" style=\"cursor: default;\">尾页</span>";
				}
				barHtml += "<input type=\"text\" id=\""+tableId+"_iptNum\" value=\""+currentPage+"\" style=\"width: 30px;height: 26px;padding: 0px;margin-left: 5px;text-align: center;\"><span class=\"paginate_button\" id=\""+tableId+"_go\" style=\"cursor: pointer;\">GO</span>";
				barHtml += "</div>";
				var startRecourdNum = (currentPage-1)*pageSize+1;
				var endRecourdNum = startRecourdNum+pageSize-1;
				if(endRecourdNum>recordAmount){
					endRecourdNum = recordAmount;
				}
				var pageDescHtml = "<div class=\"dataTables_info\" id=\""+tableId+"_info\">共 "+recordAmount+" 条数据，显示第 "+startRecourdNum+" 条至第 "+endRecourdNum+" 条</div>";
				$("#"+tableId+"_wrapper").append(pageDescHtml).append(barHtml);
				$("#"+tableId+"_paginate span[pagenum]").click(function(){
					var targetPage = $(this).attr("pagenum");
					queryData(targetPage);
				});
				$("#"+tableId+"_go").click(function(){
					var targetPage = $.trim($("#"+tableId+"_iptNum").val());
					if(targetPage!="" && /^\d+$/.test(targetPage)){
						queryData(targetPage);
					}else{
						alert("页码输入有误！");
						$("#"+tableId+"_iptNum").select();
						return;
					}
				});
			}
		};
		
		this.search = function(){	//对外公开的查询接口
			queryData();
		};
		queryData();
		return this;
	};
})(jQuery);